import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import eslintPlugin from 'vite-plugin-eslint'
import { resolve } from 'path'
const { serverConf } = require('./src/config/index')

console.log('serverConf.api=>>>>>>>>>>>')
console.log(serverConf.api)
// https://vitejs.dev/config/
export default defineConfig({
  server: {
    port: 8080, //前端服务启动的端口号
    hmr: true, //添加热更新
    https: false,
    host: 'localhost',
    open: true, // 项目启动后自动打开页面
    proxy: {
      '/api': {
        target: serverConf.api,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
    }
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
    }
  },
  plugins: [
    vue(),
    // 增加下面的配置项,这样在运行时就能检查eslint规范
    eslintPlugin({
      include: ['src/**/*.js', 'src/**/*.ts', 'src/**/*.vue', 'src/*.js', 'src/*.ts', 'src/*.vue']
    })
  ],
  base: './',
  css: {
    // css预处理器
    preprocessorOptions: {
      less: {
        charset: false,
        additionalData: '@import "./src/styles/index.less";',
      },
    },
  },
  define: {
    'process.env': process.env // vite 中 process.env已经被遗弃了需要加此配置项
  },
  build: {
    outDir: './dist',
    target: 'modules',
    sourcemap: false,
    chunkSizeWarningLimit: 1500,
    emptyOutDir: true,
    //打包环境移除console.log，debugger
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: false,
        drop_debugger: true,
      },
    },
    //打包文件按照类型分文件夹显示
    rollupOptions: {
      input: {
        main: resolve(__dirname, '/index.html'),
      },
      output: {
        chunkFileNames: 'js/[name]-[hash].js',
        entryFileNames: 'js/[name]-[hash].js',
        assetFileNames: '[ext]/[name]-[hash].[ext]',
      },
    },
  }
})
